﻿using MainFrameDesign.Action;
using MainProGrid;
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Drawing;
using System.Linq;
using System.Text;

namespace MainFrameDesign.ContentContorl
{
    public class SplitLR:Div
    {

        public SplitLR(): base()
        {
            LeftDiv.Parent = RightDiv.Parent = this;
            this.Childs.Add(LeftDiv);
            this.Childs.Add(RightDiv);
            CustomProperty pro = new CustomProperty(this, typeof(int), "中心宽度", "CenterWidth", "外观", "设置两个容器间的宽度");
            PMC.Properties.Add(pro);
            pro = new CustomProperty(this, typeof(int), "左宽度", "LeftWidth", "外观", "设置左侧容器的宽度，设置后左侧容器按照固定宽度展示");
            PMC.Properties.Add(pro);

        }
        public override string Name
        {
            get
            {
                return base.Name;
            }
            set
            {
                base.Name = value;
                LeftDiv.Name = value + "_Left";
                RightDiv.Name = value + "_Right";
            }
        }
        public override object GetValue(string name)
        {
            if (name == "CenterWidth") 
            {
                return this.CenterWidth;
            }
            if (name == "LeftWidth") 
            {
                return this.LeftWidth;
            }
            return base.GetValue(name);
        }

        public override Boolean SetValue(string name, object value)
        {
            if (base.SetValue(name, value)) 
            {
                return true;
            }
            if (name == "CenterWidth")
            {
                this.CenterWidth=(int)value;
                return true;
            }
            if (name == "LeftWidth")
            {
                this.LeftWidth=(int)value;
                return true;
            }
            return false;
        }

        private int centerWidth = 10;

        public int CenterWidth
        {
            get { return centerWidth; }
            set { centerWidth = value; }
        }

        private int leftWidth = 200;

        public int LeftWidth
        {
            get { return leftWidth; }
            set { leftWidth = value; }
        }

        public ChildDiv LeftDiv = new ChildDiv();
        public ChildDiv RightDiv = new ChildDiv();
        [Browsable(false)]
        public override Dom Root
        {
            get
            {
                return base.Root;
            }
            set
            {
                base.Root = value;
                this.LeftDiv.Root = this.RightDiv.Root = value;
            }
        }
        public override int CallHeight()
        {
            this.Height = this.Margin.Top + this.Margin.Bottom + this.Padding.Top + this.Padding.Bottom;
            int height1 = LeftDiv.CallHeight();
            int height2 = RightDiv.CallHeight();
            if (height1 > height2)
            {
                this.Height += height1;
            }
            else 
            {
                this.Height += height2;
            }
            return this.Height;
        }

        public override void CallWidth()
        {
            this.Width = this.Parent.Width - this.Parent.Margin.Left - this.Parent.Padding.Left - this.Parent.Margin.Right - this.Parent.Padding.Right;
            int width = this.Width - this.Margin.Left - this.Margin.Right - this.Padding.Left - this.Padding.Right - this.CenterWidth;
            LeftDiv.Width = LeftWidth+LeftDiv.Padding.Left+LeftDiv.Padding.Right+LeftDiv.Margin.Left+LeftDiv.Margin.Right;
            RightDiv.Width = width - LeftDiv.Width; 
            LeftDiv.CallWidth();
            RightDiv.CallWidth();
        }

        public override void CallPoint()
        {
            LeftDiv.Location.X = this.Location.X + Margin.Left + Padding.Left;
            LeftDiv.Location.Y = this.Location.Y + Margin.Top + Padding.Top;
            RightDiv.Location.X = LeftDiv.Location.X + LeftDiv.Width + CenterWidth;
            RightDiv.Location.Y = LeftDiv.Location.Y;
            LeftDiv.CallPoint();
            RightDiv.CallPoint();
        }

        public override Div AppendChild(Div div, bool isCallDraw)
        {
            throw new Exception("元素不能调用AppendChild方法");
        }

        public Div AppendChildForLeft(Div div, bool isCallDraw) 
        {
            return LeftDiv.AppendChild(div, isCallDraw);
        }

        public Div AppendChildForRight(Div div, bool isCallDraw) 
        {
            return RightDiv.AppendChild(div, isCallDraw);
        }

        public override void DrawContent(System.Drawing.Graphics g)
        {
            return;
        }

        public override string GetDom(string dom)
        {
            string tem = Environment.NewLine+ "<div id='" + this.Name + "'>";
            tem += dom;
            tem += LeftDiv.GetDom(string.Empty);
            tem += RightDiv.GetDom(string.Empty);
            tem += Environment.NewLine + "</div>";
            return tem;
        }

        public override string GetCSS(string style)
        {
            string tem = Environment.NewLine + "#" + this.Name + "{";
            tem += this.GetThisMargin();
            tem += this.GetThisPadding();
            tem += "overflow:auto;";
            tem += style;
            tem += "}";
            style = "float:left;";
            style += "width:" + LeftDiv.ContentRect.Width+ "px;";
            tem += LeftDiv.GetCSS(style);
            style = "margin-left:" + (LeftDiv.DrawRect.Width + CenterWidth) + "px;";
            tem += RightDiv.GetCSS(style);
            return tem;
        }
    }
}
